home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 273_01.zip / GETLINE.CC < prev    next >
Text File  |  1993-04-04  |  3KB  |  128 lines

  1. #include <tcutil.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. get_line(char *str, int sleng, int attr, int dleng)
  5. /* This will read s string from the screen starting at the current cursor
  6.    location.  The length of the string is specified by sleng, the length
  7.    of the display area is specified by dleng.  Attr specifies the attribute
  8.    to use when displaying the field.
  9.    RETURNED is the character code that terminated the input string.
  10.    I.E. enter key, esc key, function keys, etc.
  11. */
  12. {
  13.     int ch, ch2, idx=0, tslen=sleng, tdlen=dleng, orow, ocol, ccol;
  14.     int insert=0, lcol, odx=0, lidx, term_ch=0, x, tidx, fidx, flg=0;
  15.  
  16.     get_cur(&orow,&ocol);
  17.     show_cur(1);
  18.     rcolor(orow,ocol,attr,dleng);
  19.     ccol=ocol;
  20.     lcol=ocol + tdlen -1;
  21.     for(x=0; x <= sleng; x++)  {
  22.         if(*(str + x) == 0x00) flg=1;
  23.         if(flg) *(str + x) = ' ';
  24.         else
  25.         if(!isprint(*(str + x))) *(str + x) = ' ';
  26.     }
  27.     *(str+sleng)=0x00;
  28.     if(sleng == dleng) get_chars(orow,ocol,sleng,str);
  29.     writef_n(orow,ocol,attr,str,dleng);
  30.  
  31.     while(1) {
  32.         if(insert) show_cur(9);
  33.         else       show_cur(1);
  34.         ch=get_xa();
  35.         if(isprint(ch)) ch2=0; else ch2=ch;
  36.         switch(ch2) {
  37.             case 0:
  38.                 if(insert) {
  39.                     fidx=sleng - tslen;
  40.                     tidx=sleng - tslen +1;
  41.                     memmove(str+tidx,str+fidx,tslen-1);
  42.                 }
  43.                 *(str + idx) = ch;
  44.                 idx++; ccol++; tslen--;
  45.                 break;
  46.             case K_ENTER:
  47.             case K_ESC:
  48.                 term_ch=ch;
  49.                 goto end_it;
  50.             case K_RIGHT:
  51.                 idx++; ccol++; tslen--;
  52.                 break;
  53.             case K_HOME:
  54.                 idx=odx=0;
  55.                 tslen=sleng;
  56.                 ccol=ocol;
  57.                 break;
  58.             case K_END:
  59.                 for(idx=sleng - 1; idx >=0 ;idx--) {
  60.                     if(*(str + idx) != ' ') {
  61.                         idx++;
  62.                         break;
  63.                     }
  64.                 }
  65.                 tslen = sleng - idx;
  66.                 if(idx + 1 <= dleng) {
  67.                     odx = 0;
  68.                     ccol = ocol + idx;
  69.                 }
  70.                 else {
  71.                     odx = idx - dleng + 1;
  72.                     ccol = lcol;
  73.                 }
  74.                 break;
  75.             case K_BACKSPACE:
  76.                 if(idx) idx--;
  77.                 tslen++;
  78.                 ccol--;
  79.                 *(str + idx) = ' ';
  80.                 break;
  81.             case K_LEFT:
  82.                 idx--; tslen++;
  83.                 ccol--;
  84.                 break;
  85.             case K_CEND:
  86.                 for(x=idx; x < sleng; x++)
  87.                     *(str + x) = ' ';
  88.                 break;
  89.             case K_DEL:
  90.                 if(!tslen)
  91.                     *(str + idx) = ' ';
  92.                 else {
  93.                     tidx=sleng-tslen;
  94.                     fidx=sleng-tslen + 1;
  95.                     memmove(str+tidx,str+fidx,tslen);
  96.                     *(str+sleng -1)=' ';
  97.                 }
  98.                 break;
  99.             case K_INS:
  100.                 insert = insert ^ 0x01;
  101.                 break;
  102.             default:
  103.                 term_ch=ch;
  104.                 goto end_it;
  105.         }
  106.         if(tslen > sleng) tslen=sleng;
  107.         if(tslen < 0) tslen=0;
  108.         if(idx < 0) idx=0;
  109.         if(idx > sleng - 1) idx=sleng-1;
  110.         if(ccol > lcol) {
  111.             ccol=lcol;
  112.             odx++;
  113.         }
  114.         if(ccol < ocol) {
  115.             ccol=ocol;
  116.             odx--;
  117.         }
  118.         if(odx < 0) odx=0;
  119.         if(odx > (sleng-dleng)) odx=sleng-dleng;
  120.         writef_n(orow,ocol,attr,str+odx,dleng);
  121.         locate(orow,ccol);
  122.     }
  123. end_it:
  124.     trim_r(str);
  125.     if(!term_ch) term_ch=0x0d;
  126.     return(term_ch);
  127. }
  128.